/*
 * Copyright (c) 2004-2022, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors 
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.hisp.dhis.webapi.security;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.hisp.dhis.common.CodeGenerator;
import org.hisp.dhis.common.IdentifiableObject;
import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.document.Document;
import org.hisp.dhis.eventchart.EventChart;
import org.hisp.dhis.eventreport.EventReport;
import org.hisp.dhis.eventvisualization.EventVisualization;
import org.hisp.dhis.report.Report;
import org.hisp.dhis.sqlview.SqlView;
import org.hisp.dhis.visualization.Visualization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDecisionVoter;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.FilterInvocation;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Allows certain type/uid combinations to be externally accessed (no login required).
 *
 * @author Morten Olav Hansen <mortenoh@gmail.com>
 */
@Slf4j
@Component
public class ExternalAccessVoter implements AccessDecisionVoter<FilterInvocation> {
  // this should probably be moved somewhere else, but leaving it here for now
  private static final Map<String, Class<? extends IdentifiableObject>> externalClasses =
      new HashMap<>();

  static {
    externalClasses.put("visualizations", Visualization.class);
    externalClasses.put("maps", org.hisp.dhis.mapping.Map.class);
    externalClasses.put("reports", Report.class);
    externalClasses.put("documents", Document.class);
    externalClasses.put("sqlViews", SqlView.class);
    externalClasses.put("eventReports", EventReport.class);
    externalClasses.put("eventCharts", EventChart.class);
    externalClasses.put("eventVisualizations", EventVisualization.class);
  }

  // -------------------------------------------------------------------------
  // Dependencies
  // -------------------------------------------------------------------------

  @Autowired private IdentifiableObjectManager manager;

  // -------------------------------------------------------------------------
  // AccessDecisionVoter Implementation
  // -------------------------------------------------------------------------

  @Override
  public boolean supports(ConfigAttribute attribute) {
    return false;
  }

  @Override
  public boolean supports(Class<?> clazz) {
    return clazz.isAssignableFrom(FilterInvocation.class);
  }

  @Override
  public int vote(
      Authentication authentication,
      FilterInvocation filterInvocation,
      Collection<ConfigAttribute> attributes) {

    // TODO: MAS: refactor/remove this since we don't support usage of anonymousUser anymore
    if (authentication.getPrincipal().equals("anonymousUser")
        && authentication.isAuthenticated()
        && filterInvocation.getRequest().getMethod().equals(RequestMethod.GET.name())) {
      String requestUrl = filterInvocation.getRequestUrl();
      String[] urlSplit = requestUrl.split("/");

      if (urlSplit.length > 3) {
        String type = urlSplit[2];

        if (urlSplit[1].equals("api") && externalClasses.get(type) != null) {
          String uid = getUidPart(urlSplit[3]);

          if (CodeGenerator.isValidUid(uid)) {
            IdentifiableObject identifiableObject = manager.get(externalClasses.get(type), uid);

            if (identifiableObject != null && identifiableObject.getSharing().isExternal()) {
              log.debug("ACCESS_GRANTED [" + filterInvocation.toString() + "]");

              return ACCESS_GRANTED;
            }
          }
        }
      }
    }

    log.debug("ACCESS_ABSTAIN [" + filterInvocation.toString() + "]: No supported attributes.");

    return ACCESS_ABSTAIN;
  }

  private String getUidPart(String uidPath) {
    if (uidPath.contains(".")) {
      return uidPath.substring(0, uidPath.indexOf("."));
    }

    return uidPath;
  }
}
